#!/usr/bin/perl
# index.cgi
# Display the ucd-snmp applet

require 'snmp-lib.pl';

&header($text{'index_title'}, "", "index", 1, 1, 0);
#print "<hr>\n";

&nas_syslog("snmp","naslog_info",13000,"Now visiting the snmp module in perl");

#Get the System information
##########################################################
my $agentline = `ps -A|grep snmpd`;
if(!$agentline)
{
	&nas_syslog("snmp","naslog_info",13001,"Now starting the snmpd daemon");
	#system "snmpd";
}

my $info = `snmpwalk localhost -c public -v 1 2>&1`;
$info =~ /sysDescr.*=[ \t]*(.+)\n/;
my $sysDescrInfo = $1;
$info =~ /sysName.*=[ \t]*(.+)\n/;
my $sysNameInfo = $1;
$info =~ /sysLocation.*=[ \t]*(.+)\n/;
my $sysLocationInfo = $1;
$info =~ /sysContact.*=[ \t]*(.+)\n/;
my $sysContactInfo = $1;

if($agentline){
	$svrstat = "Running";
}
else{
	$svrstat = "Stopped";
}
print "<table class=mode1 border=0 cellpadding=1 cellspacing=0>\n";
print "<tr class=mode2><td width=100><b>Status:</b></td>",
  "<td width=150>$svrstat</td></tr></table>\n";
print  "<p><input type=submit name=srv class=button value=Start/Stop onclick='document.location=\"service.cgi\"'></p>";
#the system information section
##########################################################
print "<p><b><i>$text{'sub_title1'}</i></b></p>";

print "<table class=mode1 border=0 cellpadding=1 cellspacing=0>\n";
print "<tr class=mode2><td width=35%><b>SysName</b></td>",
  "<td width=65%>$sysNameInfo</td></tr>",
#  "<tr class=mode1><td width=35%><b>SysDescr</b></td>",
#  "<td width=65%>$sysDescrInfo</b></td></tr>",
  "<tr class=mode1><td width=35%><b>SysLocation</b></td>",
  "<td width=65%>$sysLocationInfo</b></td></tr>",
  "<tr class=mode2><td width=35%><b>SysContact</b></td>",
  "<td width=65%>$sysContactInfo</b></td></tr>",
  "</table>\n";

# an edit link for system info
#print "<p><a href=edit.cgi><b>Edit System Info</b></a></p>";
print "<p><input type=button class=button value='$text{'edit_title'}' onclick='document.location=\"edit.cgi\"'></p>";

print "<hr>";
##########################################################

# read config file content to memory
#########################################################
my $fileContent = &ReadConfFile();


#the community information section
##########################################################
print "<b><i>$text{'sub_title2'}</i></b>";

for(my $i=0; $i < @$fileContent; $i++)
{
	my $entry = $fileContent->[$i];
	if($entry =~ /^rocommunity (\w+)/)
	{
		push(@rolist, $1);
	}
	if($entry =~ /^rwcommunity (\w+)/)
	{
		push(@rwlist, $1);
	}
}

print "<table class=mode1 border=0 width=50% cellpadding=1 cellspacing=0>\n";
print "<tr class=title1><td width=50%><b>$text{'comm_name'}</b></td> ",
  "<td width=50%><b>$text{'comm_right'}</b></td></tr>";

$i = 0;
# print readonly community
foreach my $k (@rolist)
{
if ($i % 2 == 0)
{
	$dismod = "class=mode1";
}
else
{
	$dismod = "class=mode2";
}
print  "<tr $dismod><td width=50%>$k</td>",
  "<td width=50%>Read-Only</td></tr>";
$i ++;
}

#print read-write community

foreach my $k (@rwlist)
{
if ($i % 2 == 0)
{
	$dismod = "class=mode1";
}
else
{
	$dismod = "class=mode2";
}
print "<tr $dismod><td width=50%>$k</td>",
  "<td width=50%>Read-Write</td></tr>";
$i ++;
}
print "</table>";

print "<form name=comm method=post>";
# an add link for community info
#print "<p><a href=commadd.cgi><b>Add Community</b></a></p>";
print "<p><table class=mode1 border=0 width=50%>";
print "<tr><td><input type=button class=button value='$text{'commadd_title'}' onclick='document.comm.action=\"commadd.cgi\";document.comm.submit()'></td>";

# an remove link for community info
#print "<p><a href=commremove.cgi><b>Remove Community</b></a></p>";
print "<td><input type=button class=button value='$text{'commrev_title'}' onclick='document.comm.action=\"commremove.cgi\";document.comm.submit()'></td></tr></table>";

print "</form>";
print "<hr>";

##########################################################

# the trap information
##########################################################
print "<b><i>$text{'sub_title3'}</i></b>";

for(my $i=0; $i < @$fileContent; $i++)
{
	my $entry = $fileContent->[$i];
	if($entry =~ /^trapsink ([\.\w]+) (\w+)/)
	{
		$rcvlist{$1} = $2;
	}
	#get the authtrapenable info
	if($entry =~ /^authtrapenable (\d)/)
	{
		$authtrapenable = $1;
	}
}



#print keys(%rcvlist);

print "<table class=mode1 width=50% border=0 cellpadding=1 cellspacing=0>\n",
  "<tr class=title1><td width=50%><b>$text{'host'}</b></td><td witdth=50%><b>$text{'community'}</b></td></tr>";
$n = 0;
foreach my $i (keys %rcvlist)
{
if ($n % 2 == 0)
{
	$dismod = "class=mode1";
}
else
{
	$dismod = "class=mode2";
}
print "<tr $dismod><td>$i</td>","<td>$rcvlist{$i}</td></tr>";
$n ++;
}
print "</table>";

print "<form name=trap method=post>";
#print "<p><a href=trapadd.cgi><b>Add Trap Receiver</b></a></p>";
print "<p><table class=mode1 width=50% border=0>";
print "<tr><td>";
print "<input type=button class=button value='$text{'trapadd_title'}' onclick='document.trap.action=\"trapadd.cgi\";document.trap.submit()'>";
print "</td><td>";
#print "<p><a href=trapremove.cgi><b>Remove Trap Receiver</b></a></p>";
print "<input type=button class=button value='$text{'traprev_title'}' onclick='document.trap.action=\"trapremove.cgi\";document.trap.submit()'>";
print "</td><tr></table>";
print "</form>";

print "<form name=check method=post action=authenable.cgi>";
if($authtrapenable == 1)
{
print "<input type=checkbox name=checkbox value=checkbox CHECKED>",
  "Enable Authentication Trap  ";
}
else
{
print "<input type=checkbox name=checkbox value=checkbox>",
  "Enable Authentication Trap  ";
}
print "<input class=button1 type=submit value=$text{'submit'}>";
print "</form>\n";

#print "<hr>\n";
##########################################################


##########################################################

&footer("/",$text{'index'});

